/*
package com.bing.authorization.interceptor;


import com.bing.authorization.annotation.Authorization;
import com.bing.authorization.annotation.Clear;
import com.bing.authorization.user.TokenUser;
import com.bing.authorization.util.JwtUtil;
import com.bing.authorization.util.RedirectEntity;
import com.bing.response.Result;
import com.bing.response.ResultEnum;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springside.modules.mapper.JsonMapper;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

*/
/**
 * 自定义拦截器，对请求进行身份验证
 *
 * @author ScienJus
 * @date 2015/7/30.
 *//*

public class AuthorizationInterceptor extends HandlerInterceptorAdapter {

  */
/**
   * 存放登录用户模型Key的Request Key
   *//*

  public static final String REQUEST_CURRENT_USER = "REQUEST_CURRENT_USER";

  // 存放鉴权信息的Header名称，默认是Authorization
  private String httpHeaderName = "AUTHOR-TOKEN";

  private JsonMapper jsonMapper = new JsonMapper();

  // 鉴权信息的无用前缀，默认为空
  private String httpHeaderPrefix = "";

  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
          throws Exception {
    // 如果不是映射到方法直接通过
    if (!(handler instanceof HandlerMethod)) {
      return true;
    }
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Method method = handlerMethod.getMethod();
    // 从header中得到token
    String token = request.getHeader(httpHeaderName);
    if (token != null && token.startsWith(httpHeaderPrefix) && token.length() > 0) {
      token = token.substring(httpHeaderPrefix.length());
      TokenUser tokenUser = JwtUtil.parseToken(token);
      if (Objects.nonNull(tokenUser)) {
        request.setAttribute(REQUEST_CURRENT_USER, tokenUser);
        return true;
      }
    }
    boolean authorizationMethod = method.getAnnotation(Authorization.class) != null;
    boolean clearMethod = method.getAnnotation(Clear.class) == null;
    boolean authorizationClazz = handlerMethod.getBeanType().getAnnotation(Authorization.class) != null;
    if ((authorizationMethod || authorizationClazz) && clearMethod) { // 查看方法所在的Controller是否有注解
      String url = request.getScheme() + "://" + request.getServerName();//获取请求路径
      String header = request.getHeader("Accept");//获取头部信息
      boolean state = true;
      if (Objects.nonNull(header)) {
        state = header.indexOf("text/html") != -1;
      }
      if (state) {
        //获取全部请求参数名
        Enumeration<String> parameterNames = request.getParameterNames();
        StringBuffer parameter = new StringBuffer();
        //迭代全部参数，存入parameter中
        while (parameterNames.hasMoreElements()) {
          String key = parameterNames.nextElement();
          parameter.append(key);
          parameter.append("=");
          parameter.append(request.getParameter(key));
          parameter.append("&");
        }
        String path = request.getRequestURI();
        if (StringUtils.isNotBlank(parameter)) {
          //去掉最后一个&
          path = path + "?" + parameter.substring(0, parameter.length() - 1).toString();
        }
        String authUrl = RedirectEntity.getRedirectUrl(path, url);
        response.sendRedirect(authUrl);
      } else {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        BufferedWriter writer =
                new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));
        Result result = new Result(ResultEnum.UN_AUTHORIZATION);
        String message = jsonMapper.toJson(result);
        writer.write(message);
        writer.close();
        return false;
      }

    }
    // 为了防止以恶意操作直接在REQUEST_CURRENT_KEY写入key，将其设为null
    request.setAttribute(REQUEST_CURRENT_USER, null);
    return true;
  }
}
*/
